/**
 * Created by Administrator on 2016/10/16.
 */
angular.module('project.controller', [])
    .controller('projectListController', ['$charityProjectService', '$stateParams', '$sce', '$scope',
        function ($charityProjectService, $stateParams, $sce, $scope, ngDialog) {
            $scope.items = [];
            var self = this;
            this.loadProjectList = function (success) {
                $charityProjectService.getProjectList({}, function (result) {
                    //var count = 0;
                    result.data.forEach(function (item) {
                        if (item.level == 1){
                        //    if (count == 0){
                        //        count = count + 1;
                        //        item.showChildren = true;
                        //    }
                        //    else {
                                item.showChildren = false;
                        //    }
                            item.children = [];
                            $scope.items[$scope.items.length] = item;
                        }
                    });

                    result.data.forEach(function (item) {
                        if (item.level == 2){
                            $scope.items.forEach(function (parent) {
                                if (parent.id == item.parent){
                                    parent.showChildren = true;
                                    parent.children[parent.children.length] = item;
                                }
                            });
                        }
                    });
                    success();
                }, function (err) {
                    console.error(err);
                });
            };
            this.getProjectContent = function (projectId) {
                $charityProjectService.getProject({projectId: projectId}, function (result) {
                    $scope.projectId = projectId;
                    $scope.title = result.data.title;
                    $scope.content = $sce.trustAsHtml(result.data.content);
                }, function (err) {
                    console.error(err);
                });
            };
            $scope.loadContent = function (item) {
                $scope.currentItem = item;
                self.getProjectContent(item.id);
            };
            this.init = function () {
                console.log('init project list');
                self.loadProjectList(function(){
                    if ($scope.items.length > 0){
                        $scope.loadContent($scope.items[0]);
                    }
                });
            };

            $scope.toggleChild = function(item) {
                if (item.children.length > 0){
                    item.showChildren = !item.showChildren;
                }
                else{
                    $scope.loadContent(item);
                }

            };

            this.init();
        }])
    .controller('projectContentController', ['$charityProjectService', '$stateParams', '$sce', '$scope',
        function ($charityProjectService, $stateParams, $sce, $scope) {
            var projectId = $stateParams.id;
            this.queryProjectContent = function (projectId) {
                $charityProjectService.getProject({projectId: projectId}, function (result) {
                    $scope.title = result.data.title;
                    $scope.content = $sce.trustAsHtml(result.data.content);
                }, function (err) {
                    console.error(err);
                });
            };
            this.init = function (projectId) {
                console.log('init');
                this.queryProjectContent(projectId);
            };
            this.init(projectId);
        }]);